##############
## American Identity, Status Threat, and Backlash - Open Responses Studies 1 and 4, Public
##############

##############
# S1 ----------------------------------------------------------------------
##############
#####
## How easy was it to come up with shared values?
#####
table(S1_D3$Condition)
table(S1_D3$Condition_num)
S1_D3_Value_Data <- subset(S1_D3, Condition_num %in% c(3))
nrow(S1_D3_Value_Data)
colnames(S1_D3)
#tibble::view(S1_D3_Value_Data$Unique) ## no NAs. DKs hard to evaluate

#####
## Effects of specific contents?
#####
## effect of mentioning "family"
sum(grepl("\\bfamily\\b", S1_D3_Value_Data$Unique, ignore.case = TRUE)) ## 9 mentioned family

S1_D3_Value_Data$family_mentioned <- ifelse(grepl("\\bfamily\\b", S1_D3_Value_Data$Unique, ignore.case = TRUE), 1, 0)
table(S1_D3_Value_Data$family_mentioned)
summary(lm(mean_Status_Threat_scaled ~ factor(family_mentioned), S1_D3_Value_Data))
summary(lm(mean_American_Identity_scaled ~ factor(family_mentioned), S1_D3_Value_Data))
summary(lm(mean_White_Identity_scaled ~ factor(family_mentioned), S1_D3_Value_Data))

## effect of mentioning "freedom"
sum(grepl("\\bfreedom\\b", S1_D3_Value_Data$Unique, ignore.case = TRUE)) ## 170 mentioned freedom
sum(stringr::str_count(S1_D3_Value_Data$Unique, "\\bfreedom\\b"), na.rm = TRUE) ## 247 times freedom mentioned in total

sum(stringr::str_count(S1_D3$Unique, "\\bfreedoms\\b"), na.rm = TRUE)


S1_D3_Value_Data$freedom_mentioned <- ifelse(grepl("\\bfreedom\\b", S1_D3_Value_Data$Unique, ignore.case = TRUE), 1, 0)
table(S1_D3_Value_Data$freedom_mentioned)
summary(lm(mean_Status_Threat_scaled ~ factor(freedom_mentioned), S1_D3_Value_Data))
summary(lm(mean_American_Identity_scaled ~ factor(freedom_mentioned), S1_D3_Value_Data))
summary(lm(mean_White_Identity_scaled ~ factor(freedom_mentioned), S1_D3_Value_Data))

## effect of mentioning "equality"
sum(grepl("\\bequality\\b", S1_D3_Value_Data$Unique, ignore.case = TRUE)) ## 97 mentioned equality

S1_D3_Value_Data$equality_mentioned <- ifelse(grepl("\\bequality\\b", S1_D3_Value_Data$Unique, ignore.case = TRUE), 1, 0)
table(S1_D3_Value_Data$equality_mentioned)
summary(lm(mean_Status_Threat_scaled ~ factor(equality_mentioned), S1_D3_Value_Data))
summary(lm(mean_American_Identity_scaled ~ factor(equality_mentioned), S1_D3_Value_Data))
summary(lm(mean_White_Identity_scaled ~ factor(equality_mentioned), S1_D3_Value_Data))

## effect of mentioning "democracy"
sum(grepl("\\bdemocracy\\b", S1_D3_Value_Data$Unique, ignore.case = TRUE)) ## 65 mentioned democracy

S1_D3_Value_Data$democracy_mentioned <- ifelse(grepl("\\bdemocracy\\b", S1_D3_Value_Data$Unique, ignore.case = TRUE), 1, 0)
table(S1_D3_Value_Data$democracy_mentioned)
summary(lm(mean_Status_Threat_scaled ~ factor(democracy_mentioned), S1_D3_Value_Data))
summary(lm(mean_American_Identity_scaled ~ factor(democracy_mentioned), S1_D3_Value_Data))
summary(lm(mean_White_Identity_scaled ~ factor(democracy_mentioned), S1_D3_Value_Data))

##############
# S4 ----------------------------------------------------------------------
##############
view(S4_D3$Values_all)
view(S4_D3$AmHardship_all)

#####
## How easy was it to come up with shared values?
#####
table(S4_D3$Condition)
table(S4_D3$Condition_num)
S4_D3_Value_Data <- subset(S4_D3, Condition_num %in% c(2))
nrow(S4_D3_Value_Data)

#tibble::view(S4_D3_Value_Data$Values_all) ## no NAs. DKs hard to evaluate

#####
## Effects of specific contents?
#####
## effect of mentioning "family"
sum(grepl("\\bfamily\\b", S4_D3_Value_Data$Values_all, ignore.case = TRUE)) ## 117 mentioned family

S4_D3_Value_Data$family_mentioned <- ifelse(grepl("\\bfamily\\b", S4_D3_Value_Data$Values_all, ignore.case = TRUE), 1, 0)
table(S4_D3_Value_Data$family_mentioned)
summary(lm(mean_Status_Threat_scaled ~ factor(family_mentioned), S4_D3_Value_Data))
summary(lm(mean_American_Identity_scaled ~ factor(family_mentioned), S4_D3_Value_Data))
summary(lm(mean_White_Identity_scaled ~ factor(family_mentioned), S4_D3_Value_Data))

## effect of mentioning "freedom"
sum(grepl("\\bfreedom\\b", S4_D3_Value_Data$Values_all, ignore.case = TRUE)) ## 117 mentioned equality
S4_D3_Value_Data$freedom_mentioned <- ifelse(grepl("\\bfreedom\\b", S4_D3_Value_Data$Values_all, ignore.case = TRUE), 1, 0)
table(S4_D3_Value_Data$freedom_mentioned)
summary(lm(mean_Status_Threat_scaled ~ factor(freedom_mentioned), S4_D3_Value_Data))
summary(lm(mean_American_Identity_scaled ~ factor(freedom_mentioned), S4_D3_Value_Data))
summary(lm(mean_White_Identity_scaled ~ factor(freedom_mentioned), S4_D3_Value_Data))

## effect of mentioning "equality"
sum(grepl("\\bequality\\b", S4_D3_Value_Data$Values_all, ignore.case = TRUE)) ## 117 mentioned equality
S4_D3_Value_Data$equality_mentioned <- ifelse(grepl("\\bequality\\b", S4_D3_Value_Data$Values_all, ignore.case = TRUE), 1, 0)
table(S4_D3_Value_Data$equality_mentioned)
summary(lm(mean_Status_Threat_scaled ~ factor(equality_mentioned), S4_D3_Value_Data))
summary(lm(mean_American_Identity_scaled ~ factor(equality_mentioned), S4_D3_Value_Data))
summary(lm(mean_White_Identity_scaled ~ factor(equality_mentioned), S4_D3_Value_Data))

## effect of mentioning "democracy"
sum(grepl("\\bdemocracy\\b", S4_D3_Value_Data$Values_all, ignore.case = TRUE)) ## 117 mentioned democracy
S4_D3_Value_Data$equality_mentioned <- ifelse(grepl("\\bdemocracy\\b", S4_D3_Value_Data$Values_all, ignore.case = TRUE), 1, 0)
table(S4_D3_Value_Data$equality_mentioned)
summary(lm(mean_Status_Threat_scaled ~ factor(equality_mentioned), S4_D3_Value_Data))
summary(lm(mean_American_Identity_scaled ~ factor(equality_mentioned), S4_D3_Value_Data))
summary(lm(mean_White_Identity_scaled ~ factor(equality_mentioned), S4_D3_Value_Data))

#####
## Do the groups people thought about predict outcomes?
#####
# Recode the specified columns to binary 1/0
identity_columns <- c("Identity_all1_1", "Identity_all1_2", "Identity_all1_4", 
                      "Identity_all1_5", "Identity_all1_6", "Identity_all1_7", 
                      "Identity_all1_8", "Identity_all1_9", "Identity_all1_10", 
                      "Identity_all1_11", "Identity_all1_13", "Identity_all1_14", 
                      "Identity_all1_15", "Identity_all1_16")

S4_D3 <- S4_D3 %>% mutate(across(all_of(identity_columns), ~ ifelse(!is.na(.) & . != 0, 1, 0)))
colnames(S4_D3)
table(S4_D3$Identity_all1_1)

## run regressions for the relevant variables
group_reg_1 <- lm(mean_Status_Threat_scaled ~ Identity_all1_1 + Identity_all1_2 + Identity_all1_4 + Identity_all1_5 + Identity_all1_6 + Identity_all1_7 + 
             Identity_all1_8 + Identity_all1_9 + Identity_all1_10 + Identity_all1_11 + Identity_all1_13 + Identity_all1_14 + Identity_all1_15 + 
             Identity_all1_16, data = S4_D3) ## status threat
group_reg_2 <- lm(mean_American_Identity_scaled ~ Identity_all1_1 + Identity_all1_2 + Identity_all1_4 + Identity_all1_5 + Identity_all1_6 + Identity_all1_7 + 
                    Identity_all1_8 + Identity_all1_9 + Identity_all1_10 + Identity_all1_11 + Identity_all1_13 + Identity_all1_14 + Identity_all1_15 + 
                    Identity_all1_16, data = S4_D3) ## American identity
group_reg_3 <- lm(mean_White_Identity_scaled ~ Identity_all1_1 + Identity_all1_2 + Identity_all1_4 + Identity_all1_5 + Identity_all1_6 + Identity_all1_7 + 
                    Identity_all1_8 + Identity_all1_9 + Identity_all1_10 + Identity_all1_11 + Identity_all1_13 + Identity_all1_14 + Identity_all1_15 + 
                    Identity_all1_16, data = S4_D3) ## White identity
group_reg_4 <- lm(mean_Symbolic_Patriotism_scaled ~ Identity_all1_1 + Identity_all1_2 + Identity_all1_4 + Identity_all1_5 + Identity_all1_6 + Identity_all1_7 + 
                    Identity_all1_8 + Identity_all1_9 + Identity_all1_10 + Identity_all1_11 + Identity_all1_13 + Identity_all1_14 + Identity_all1_15 + 
                    Identity_all1_16, data = S4_D3) ## Symbolic patriotism
group_reg_5 <- lm(mean_Constructive_Patriotism_scaled ~ Identity_all1_1 + Identity_all1_2 + Identity_all1_4 + Identity_all1_5 + Identity_all1_6 + Identity_all1_7 + 
                    Identity_all1_8 + Identity_all1_9 + Identity_all1_10 + Identity_all1_11 + Identity_all1_13 + Identity_all1_14 + Identity_all1_15 + 
                    Identity_all1_16, data = S4_D3) ## Constructive patriotism
group_reg_6 <- lm(mean_Nationalism_scaled ~ Identity_all1_1 + Identity_all1_2 + Identity_all1_4 + Identity_all1_5 + Identity_all1_6 + Identity_all1_7 + 
                    Identity_all1_8 + Identity_all1_9 + Identity_all1_10 + Identity_all1_11 + Identity_all1_13 + Identity_all1_14 + Identity_all1_15 + 
                    Identity_all1_16, data = S4_D3) ## Nationalism
group_reg_7 <- lm(mean_National_Pride_scaled ~ Identity_all1_1 + Identity_all1_2 + Identity_all1_4 + Identity_all1_5 + Identity_all1_6 + Identity_all1_7 + 
                    Identity_all1_8 + Identity_all1_9 + Identity_all1_10 + Identity_all1_11 + Identity_all1_13 + Identity_all1_14 + Identity_all1_15 + 
                    Identity_all1_16, data = S4_D3) ## National pride


## Plot this
?plot_coefs
library(jtools)
group_reg_plot <- plot_coefs(group_reg_2,group_reg_3,group_reg_4,group_reg_5,group_reg_6,group_reg_7,
                             group_reg_1, 
                                 scale = FALSE,
                                 model.names = c("American identity","White identity","Symbolic patriotism","Constructive patriotism","Nationalism","National pride","Status threat"),
                                 coefs = c("People like me" = "Identity_all1_1","Americans" = "Identity_all1_2","People in need" = "Identity_all1_4",
                                           "No group at all" = "Identity_all1_5","White people" = "Identity_all1_6","People of Color" = "Identity_all1_7",
                                           "Humans in general" = "Identity_all1_8","Conservatives" = "Identity_all1_9","Liberals" = "Identity_all1_10",
                                           "Friends and family" = "Identity_all1_11","People with similar food preferences" = "Identity_all1_13","Workers" = "Identity_all1_14",
                                           "I thought about myself" = "Identity_all1_15","Other groups" = "Identity_all1_16"),
                                 exp = FALSE,
                                 robust = TRUE,
                                 point.shape = TRUE,
                                 colors = c("black","darkgray","darkgray","darkgray","darkgray","darkgray",
                                            "darkgreen"))  +
  theme(
    legend.position = "right",
    axis.text.y = element_text(size = 40),      # Increase size of coefficient names on y-axis
    axis.title = element_text(size = 40),
    axis.text.x = element_text(size = 40),
    legend.text = element_text(size = 40),
    legend.title = element_text(size = 40))
ggsave("Groups_Plot.jpg", scale = 1, dpi="retina", dev='jpeg', height=30, width=45, units="cm") 



##############
## End of the syntax. 